package com.zj.ecology.activity

import android.support.v4.app.FragmentTransaction
import android.view.KeyEvent
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.LinearLayout

import com.zj.base.LayoutModule
import com.zj.base.base_act.BaseActivity
import com.zj.base.base_fg.BaseFragmentManager
import com.zj.base.utils.ToastUtils
import com.zj.base.view_injector.annotantions.IdRes
import com.zj.ecology.R
import com.zj.ecology.fragments.MainFragments
import com.zj.ecology.fragments.SecondFragments
import com.zj.ecology.fragments.ThirdFragments
import com.zj.ecology.interfaces.FragmentDataSet
import com.zj.ecology.views.SelectChangeIndexView
import com.zj.weather.DynamicWeatherBackground

/**
 * Created by zhaojie on 2018/7/13.
 */

class IndexActivity : BaseActivity() {

    @IdRes(R.id.index_ll_index_parent)
    private val llIndexParent: LinearLayout? = null

    private var baseFragmentManager: BaseFragmentManager? = null

    private var curTime: Long = 0

    private val mainFragment = MainFragments()
    private val secondFragment = SecondFragments()
    private val thirdFragments = ThirdFragments()

    override fun setTitle(): String {
        return ""
    }

    override fun isTitleBarShow(): Boolean {
        return false
    }

    override fun getContentView(): LayoutModule {
        return LayoutModule(R.layout.activity_index, false, false)
    }

    override fun initView() {
        baseFragmentManager = object : BaseFragmentManager(this, llIndexParent, R.id.index_fl_content, 0, mainFragment, secondFragment, thirdFragments) {
            override fun onViewAttach(v: View) {
                if (v is SelectChangeIndexView) {
                    v.init()
                }
            }

//            override fun beginTransaction(transaction: FragmentTransaction, oldItem: Int, curItem: Int, maxIndex: Int) {
//                if ((oldItem == maxIndex && curItem == 0) || (oldItem == 0 && curItem == maxIndex)) {
//                    //最后一个往第一个或第一个往最后一个
//                    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
//                } else {
//                    //从左向右还是从右向左
//                    if (oldItem < curItem) {
//                        transaction.setCustomAnimations(R.anim.main_frg_enter_plus, R.anim.main_frg_eixt_plus)
//                    } else {
//                        transaction.setCustomAnimations(R.anim.main_frg_enter_reduce, R.anim.main_frg_eixt_reduce)
//                    }
//                }
//            }
        }
    }

    override fun initListener() {
//        baseFragmentManager?.setFragmentObserver({ triggerFragment, isHidden, stateChange ->
//            if (secondFragment != triggerFragment) {
//                stateChange.change()
//            } else
//                if (isHidden) {
//
//                    stateChange.change()
//                } else {
//                    stateChange.change()
//
//                }
//        })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - curTime > 500) {
                ToastUtils.show("再次轻击返回可退出应用")
                curTime = System.currentTimeMillis()
                return false
            } else {
                return super.onKeyDown(keyCode, event)
            }
        }
        return super.onKeyDown(keyCode, event)
    }

}
